// pages/my/my.ts
const api = require('../../utils/api');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        items: [
            {
                id: 1,
                image: '../../assets/images/icon1.png', // 替换为实际图片链接
                text: '超市订单',
                url: '/pages/myorder/myorder' // 替换为实际页面路径
            },
            {
                id: 2,
                image: '../../assets/images/icon3.png',  // 替换为实际图片链接
                text: '邀请海报',
                url: '/pages/invite/invite' // 替换为实际页面路径
            },
            {
                id: 3,
                image: '../../assets/images/icon4.png',
                text: '联系客服',
                url: '/pages/linkpage/linkpage' // 替换为实际页面路径
            },
            {
                id: 1,
                image: '../../assets/images/icon5.png', // 替换为实际图片链接
                text: '我的粉丝',
                url: '/pages/myfance/myfance' // 替换为实际页面路径
            },
            {
                id: 2,
                image: '../../assets/images/icon2.png',  // 替换为实际图片链接
                text: '我的分店',
                url: '/pages/mybranch/mybranch' // 替换为实际页面路径
            },
            {
                id: 3,
                image: '../../assets/images/icon6.png',
                text: '银行卡',
                url: '/pages/bankcardlist/bankcardlist'
            },
        ],
        avatar: '',
        nickName: '先生',
        phonenumber: '',
        propertyAmount: 0,
        isBranch:0,//是否是分店 0：否 1：是
        isLogin:false
    },
    withdrawallistFn() {
        wx.navigateTo({
            url: '/pages/withdrawallist/withdrawallist'
        })
    },
    goEditFn() {
        wx.navigateTo({
            url: '/pages/myedit/myedit'
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
       
    },
    getUserInfo() {
        let that = this
        api.getInfo().then(res => {
            if (res.code == 200) {
                let data = res.data.user
                wx.setStorageSync('userInfo', data)
                that.setData({
                    avatar: data.avatar || '',
                    nickName: data.nickName,
                    phonenumber: data.phonenumber,
                    propertyAmount: data.propertyAmount || 0,
                    isBranch:data.isBranch || 0
                })
            }
        })
    },
    goGridDetail(event){
      const item = event.currentTarget.dataset.item;
        if(!this.data.isLogin){
            wx.showModal({
                title: '您还未登录',
                content: '请先登录再进行操作~',
                showCancel: true, // 不显示取消按钮
                confirmText: '去登录',
                cancelText:'暂不登录',
                confirmColor: '#91CFDC',
                success(res) {
                  if (res.confirm) {
                    wx.navigateTo({
                        url: '/pages/login/login'
                    })
                  }
                }
            });
        }else{
            wx.navigateTo({
                url: item.url
            })  
        }
    },
    exitLogin(){
      let that = this
      wx.showModal({
        title: '提示',
        content: '退出登录后将不能使用平台相关功能~',
        showCancel: true, 
        confirmText: '继续退出',
        cancelText:'暂不退出',
        confirmColor: '#91CFDC',
        success(res) {
          if (res.confirm) {
            wx.removeStorageSync('token')
            wx.setStorageSync('ExitLogin',1)
            that.setData({
              isLogin:false
            })
          }
        }
      });
     
    },
    goLogin(){
        wx.showModal({
            title: '您还未登录',
            content: '请先登录再进行操作~',
            showCancel: true, // 不显示取消按钮
            confirmText: '去登录',
            cancelText:'暂不登录',
            confirmColor: '#91CFDC',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                    url: '/pages/login/login'
                })
              }
            }
          });
    },
    tixianFn() {
        wx.navigateTo({
            url: '/pages/withdrawal/withdrawal'
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if(!wx.getStorageSync('token')){
            this.setData({
                isLogin:false
            })
        }else{
            this.setData({
                isLogin:true
            })
        }
        if(this.data.isLogin){
            this.getUserInfo()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})